/**
 * 
 */
package rachauto.presentation.filters;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import rachauto.presentation.controllers.UserController;

/**
 * @author said
 *
 */
/* @WebFilter(urlPatterns={ "/faces/views/*","/faces/templates/*" } ) */
public class AccessFilter implements Filter {
	
	
	private UserController userController;

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		
		 HttpServletRequest req = (HttpServletRequest) request;
		 // AuthorizationBean authorization = (Authorization) req.getSession().getAttribute("authorization");
		 
		 
		 boolean isUserLogedIn = userController.isUserLogedIn();
	        if (isUserLogedIn) {
	            // User is logged in, so just continue request.
	            chain.doFilter(request, response);
	        } else {
	            // User is not logged in, so redirect to index.
	            HttpServletResponse res = (HttpServletResponse) response;
	            res.sendRedirect(req.getContextPath() + "/faces/index.xhtml");
	     }
		
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}

	/**
	 * @return the userController
	 */
	public UserController getUserController() {
		return userController;
	}

	/**
	 * @param userController the userController to set
	 */
	public void setUserController(UserController userController) {
		this.userController = userController;
	}

}
